using SDK.Scripts.Utils;
using System;
using System.Net;
using System.Runtime.InteropServices;

namespace SDK.Scripts.Network
{
	[Serializable]
	[StructLayout(LayoutKind.Sequential, Pack = 1)]
	public class MsgHead : BaseMsg
	{
		public int mLen;

		public byte mType;

		public short mReserve;

		public int mCmd;

		public int mSeq;

		public byte mOption;

		public override bool encodeHead(ref byte[] bytes)
		{
			int num = 0;
			ConvertByte(mLen, bytes, num);
			num += Marshal.SizeOf(mLen.GetType());
			ConvertByte(mType, bytes, num);
			num += Marshal.SizeOf(mType.GetType());
			ConvertByte(mOption, bytes, num);
			num += Marshal.SizeOf(mOption.GetType());
			ConvertByte(mReserve, bytes, num);
			num += Marshal.SizeOf(mReserve.GetType());
			ConvertByte(mCmd, bytes, num);
			num += Marshal.SizeOf(mCmd.GetType());
			ConvertByte(mSeq, bytes, num);
			num += Marshal.SizeOf(mSeq.GetType());
			return true;
		}

		public override bool decodeHead(ref byte[] bytes)
		{
			int num = Marshal.SizeOf(typeof(MsgHead));
			if (bytes.Length < num)
			{
				return false;
			}
			int startIndex = 0;
			byte[] byteResult = null;
			if (SpliteHeadMsg(mLen, bytes, ref startIndex, out byteResult))
			{
				mLen = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(byteResult, 0));
				if (SpliteHeadMsg(mType, bytes, ref startIndex, out byteResult))
				{
					mType = byteResult[0];
					if (SpliteHeadMsg(mOption, bytes, ref startIndex, out byteResult))
					{
						mOption = byteResult[0];
						if (SpliteHeadMsg(mReserve, bytes, ref startIndex, out byteResult))
						{
							mReserve = BitConverter.ToInt16(byteResult, 0);
							if (SpliteHeadMsg(mCmd, bytes, ref startIndex, out byteResult))
							{
								mCmd = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(byteResult, 0));
								if (SpliteHeadMsg(mSeq, bytes, ref startIndex, out byteResult))
								{
									mSeq = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(byteResult, 0));
									if (mLen < 0)
									{
										Debuger.LogError($"len={mLen},mType={mType},mOption={mOption},mReserve={mReserve},mCmd={mCmd},mSeq={mSeq}");
										return false;
									}
									return true;
								}
								return false;
							}
							return false;
						}
						return false;
					}
					return false;
				}
				return false;
			}
			return false;
		}
	}
}
